/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.spms.service.dao.impl;

import com.wisnu.spms.service.dao.DbEmployeeDao;
import com.wisnu.spms.service.entity.DbEmployee;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class DbEmployeeDaoImpl extends HibernateDaoSupport implements DbEmployeeDao {

    @Override
    @Transactional(readOnly = true)
    public DbEmployee getEmployee(final String username, final String password) throws Exception {
        try {
            Object o = getHibernateTemplate().execute(new HibernateCallback() {

                @Override
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    return session.createQuery("select a from DbEmployee a where a.employeeUserName = :user AND a.employeePassword = :pass AND a.employeeIsActive = :active")
                            .setMaxResults(1)
                            .setParameter("user", username)
                            .setParameter("pass", password)
                            .setParameter("active", "Yes")
                            .uniqueResult();
                }
            });
            if (o == null) {
                //throw new Exception("Student with ID " + id + " do not found");
                return null;
            } else {
                return (DbEmployee) o;
            }
        } catch (DataAccessException t) {
            throw new Exception(t.getMessage());
        }
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void updateEmployee(DbEmployee dbEmployee) throws Exception {
        try {
            getHibernateTemplate().update(dbEmployee);
        } catch (DataAccessException t) {
            throw new Exception(t.getMessage());
        }
    }

    @Override
    @Transactional(readOnly = true)
    public DbEmployee getEmployee() throws Exception {
        try {
            Object o = getHibernateTemplate().execute(new HibernateCallback() {

                @Override
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    return session.createQuery("select a from DbEmployee a where a.employeeIsLogin = :login AND a.employeeIsActive = :active")
                            .setMaxResults(1)
                            .setParameter("login", "Yes")
                            .setParameter("active", "Yes")
                            .uniqueResult();
                }
            });
            if (o == null) {
                //throw new Exception("Student with ID " + id + " do not found");
                return null;
            } else {
                return (DbEmployee) o;
            }
        } catch (DataAccessException t) {
            throw new Exception(t.getMessage());
        }
    }

}
